package org.middleware.controller.system;

import lombok.extern.slf4j.Slf4j;
import org.middleware.common.AuthenticationToken.RequireLogin;
import org.middleware.common.BusinessException;
import org.middleware.controller.common.CommonHttpResponse;
import org.middleware.entity.system.SysSystems;
import org.middleware.service.system.SysSystemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequireLogin
public class SysSystemsController {

    @Autowired
    private SysSystemsService systemsService;

    @GetMapping("/systems/subscribers/")
    public CommonHttpResponse subscribers(@RequestAttribute("userId") String userId) {
        try {
            List<SysSystems> systems = systemsService.subscribers(Integer.parseInt(userId));
            return CommonHttpResponse.success(systems);
        }catch (Exception e) {
            log.error("查询订阅的系统异常：", e);
            return CommonHttpResponse.fail(5500, "系统异常，请联系管理员");
        }
    }

    @GetMapping("/systems")
    public CommonHttpResponse getAllSystems() {
        try{
            List<SysSystems> result = systemsService.getAllSystems();
            return CommonHttpResponse.success(result);
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        }catch (Exception e) {
            log.error("根据名称查询服务信息异常：", e);
            return CommonHttpResponse.fail(500, "查询服务信息异常");
        }
    }

    @GetMapping("/systems/{name}")
    public CommonHttpResponse getSystemsByName(@PathVariable String name) {
        try{
            List<SysSystems> result = systemsService.getSystemsByName(name);
            return CommonHttpResponse.success(result);
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        }catch (Exception e) {
            log.error("根据名称查询服务信息异常：", e);
            return CommonHttpResponse.fail(500, "查询服务信息异常");
        }
    }

    @PostMapping("/systems")
    public CommonHttpResponse create(@RequestBody SysSystems system) {
        try{
            systemsService.create(system);
            return CommonHttpResponse.success("新增成功");
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        }catch (Exception e) {
            log.error("新增服务信息异常：", e);
            return CommonHttpResponse.fail(500, "新增服务信息异常");
        }
    }

    @PutMapping("/systems")
    public CommonHttpResponse update(@RequestBody SysSystems system) {
        try{
            systemsService.update(system);
            return CommonHttpResponse.success("修改服务成功");
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        }catch (Exception e) {
            log.error("修改服务信息异常：", e);
            return CommonHttpResponse.fail(500, "修改服务信息异常");
        }
    }

    @DeleteMapping("/systems/{id}")
    public CommonHttpResponse delete(@PathVariable int id) {
        try{
            systemsService.delete(id);
            return CommonHttpResponse.success("删除服务成功");
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        }catch (Exception e) {
            log.error("删除服务信息异常：", e);
            return CommonHttpResponse.fail(500, "删除服务信息异常");
        }
    }

}
